<?php
/**
 * Created by PhpStorm.
 * User: 张辉
 * Date: 2018/9/12
 * Time: 14:31
 */
namespace app\common\service;
use app\common\library\CommonFun;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
class RabbitMqService
{
    /**
     * Description:MQ连接句柄
     */
    private $db;
    public $connection;

    public function __construct()
    {
        CommonFun::get_other_config("RabbitMqConfig",2);
        $RabbitMqConfig = config("RabbitMq");

        $this->connection = new AMQPStreamConnection($RabbitMqConfig['RMQ_HOST'], $RabbitMqConfig['RMQ_PROT'], $RabbitMqConfig['RMQ_USER'], $RabbitMqConfig['RMQ_PWD'],$RabbitMqConfig['RMQ_VHOST']);
        $this->db = $this->connection->channel();
    }

    /**
     * 构建普通队列
     * @param string $queue 队列名称
     * @param string $body  存储的消息
     * @param bool   $passive
     * @param bool   $durable
     * @param bool   $exclusive
     * @param bool   $auto_delete
     */
    public function set_queue_basic_publish( $queue, $body, $passive = false, $durable = false, $exclusive = false, $auto_delete = false  )
    {
        $this->db->queue_declare($queue, $passive, $durable, $exclusive, $auto_delete);
//        $this->db->exchange_declare('coin_price_alarm', 'fanout',false,false,false);
        $msg = new AMQPMessage( $body );
        $this->db->basic_publish( $msg, 'coin_price_alarm', $queue );
    }

    /**
     * Description:获取MQ类
     */
    public function GetDbConnect()
    {
        return $this->db;
    }

    /**
     * 释放链接
     */
    public function __destruct(){
        $this->db->close();
        $this->connection->close();
    }


}